<?php
include('../../include/functions.inc.php');
include('../../include/conversor.php');
require_once('../../include/tcpdf/tcpdf.php');

$strSerie = getParam("SERIE");
$strNumero = getParam("NUMERO");
$strFecha = date("d").'          '.date("M").'          '.date("Y");
$sinTotal = 0; 

$f = $db->query_select("cllg_factura", "SERIE= " . sqlValue($strSerie, "text") . " AND NUMERO=" . sqlValue($strNumero, "int"));
$h = $f[0];
$asign = array();
$fullname = "";
$career = "";
if ($h["FK_STUDENT"] != NULL) {
    $stud = $asign = $db->queryToArray("select a.FIRST_NAME,a.SECOND_NAME,a.LAST_NAME from cllg_student a where a.ID=" . $h["FK_STUDENT"]);
    $fullname = $stud[0]["FIRST_NAME"] . " " . $stud[0]["SECOND_NAME"] . " " . $stud[0]["LAST_NAME"];
    $asign = $db->query_toArray("select a.FK_CAREER,b.NAME from cllg_student_assign a,cllg_career b where a.FK_CAREER=b.ID AND a.FK_STUDENT=" . $h["FK_STUDENT"]);
    $career = $asign[0]["NAME"];
}
$d = $db->queryToArray(sprintf("SELECT *
                                FROM cllg_mov_cargo
                                WHERE FK_FACTURA=%s", sqlValue($h['ID'], "int")));


    
    $pdf = new TCPDF("", "mm", "LETTER", true, 'UTF-8', false);
    $pdf->setPageOrientation("");
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetMargins(0, 0, 0);
    
    // set font
    $pdf->SetFont('helvetica', '', 11);

    // add a page
    $pdf->AddPage();

    // set cell padding
    $pdf->setCellPaddings(0, 0, 0, 0);

    // set cell margins
    $pdf->setCellMargins(0, 0, 0, 0);

    // set color for background
    $pdf->SetFillColor(0, 0, 0, 0);

    // MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)
    
    //Serie y Numero 
//  $pdf->MultiCell(20, 1, $strSerie.' - '.$strNumero, 0, 'L', 0, 1, 140, 6, true);
    //Fecha
  //  $pdf->MultiCell(80, 1, $strFecha, 0, 'L', 0, 1, 141, 19, true);
    //Nombre
    //$pdf->MultiCell(130, 1, $h["NOMBRE"], 0, 'L', 0, 1, 30, 27, true);
    //Direccion
    //$pdf->MultiCell(125, 1, $h["DIRECCION"], 0, 'L', 0, 1, 35, 32, true);
    //NIT
    //$pdf->MultiCell(35, 1, $h["NIT"], 0, 'L', 0, 1, 163, 37, true);
    
    //Detalle
   /* $intY = 48;
    foreach ($d as $i) {
        $pdf->MultiCell(90, 1, $i["DESCRIPCION"], 0, 'L', 0, 1, 24, $intY, true);
        $pdf->MultiCell(20, 1, number_format($i["SUBTOTAL"], 2), 0, 'R', 0, 1, 190, $intY, true);
        $intY += 4;
        $sinTotal += $i["SUBTOTAL"];    
    }
    
    $pdf->MultiCell(120, 2, $fullname, 0, 'C', 0, 1, 20, 67, true);
    $pdf->MultiCell(120, 2, $career, 0, 'C', 0, 1, 20, 70, true);
    
    $pdf->MultiCell(90, 2, convertir($sinTotal), 0, 'L', 0, 1, 24, 73, true);
    $pdf->MultiCell(20, 1, number_format($sinTotal, 2), 0, 'R', 0, 1, 190, 76, true);*/

    //Serie y Numero 
    $pdf->MultiCell(20, 1, $strSerie.' - '.$strNumero, 0, 'L',  false, 1, 185, 5, true);
    //Fecha
    $pdf->MultiCell(70, 1, $strFecha, 0, 'L', 0, 1, 156, 18, true);
    //Nombre
    $pdf->MultiCell(130, 1, $h["NOMBRE"], 0, 'L', 0, 1, 29, 25, true);
    //Direccion
    $pdf->MultiCell(125, 1, $h["DIRECCION"], 0, 'L', 0, 1, 29, 29, true);
    //NIT
    $pdf->MultiCell(35, 1, $h["NIT"], 0, 'L', 0, 1, 185, 33, true);
    
    //Detalle
    $intY = 46;
    $index = 0;
    while (count($d) > $index) {
        $des = $db->query_select("cllg_cargo",sprintf("ID=%s", $d[$index]['FK_CARGO']));
        $pdf->MultiCell(90, 1, $des[0]["DESCRIPTION"], 0, 'L', 0, 1, 10, $intY, true);
        $pdf->MultiCell(20, 1, number_format($d[$index]["MONTO"], 2), 0, 'R', 0, 1, 195, $intY, true);
        $intY += 4;
        $sinTotal += $d[$index]["MONTO"];
        $index++;
    }
    $strTotal = $sinTotal;
    $arrExplode = explode('.', $strTotal);
    $pdf->MultiCell(120, 2, $fullname, 0, 'C', 0, 1, 45, 67, true);
    $pdf->MultiCell(120, 2, $career, 0, 'C', 0, 1, 45, 71, true);
    
    $pdf->MultiCell(90, 2, ucfirst(PrecioEnLetras($sinTotal)), 0, 'L', 0, 1, 24, 75, true);
    $pdf->MultiCell(20, 1, number_format($sinTotal, 2), 0, 'R', 0, 1, 195, 75, true);
    
    // force print dialog
    //$js = 'print(true);';

    // set javascript
    //$pdf->IncludeJS($js);
    
    $pdf->Output('Factura '. $strNumero .'.pdf', 'I');
?>
